package org.blueoxygen.kukuruyuk.actions;

import java.sql.SQLException;
import org.blueoxygen.cimande.persistence.hibernate.HibernateSessionFactory;
import org.blueoxygen.cimande.persistence.hibernate.HibernateSessionFactoryAware;
import org.blueoxygen.kukuruyuk.entity.PurchaseOrder;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;

public class SearchPurchaseOrder extends PurchaseOrderForm
	implements HibernateSessionFactoryAware {
	private HibernateSessionFactory hsf;
	private Session sess;
	private int maxPage, currPage, nextPage, prevPage = 0, page = 0;
	private int maxRowPerPage = 10;
	private String orderBy = "name";
	private int resultRows;
	public String execute(){
		sess = hsf.createSession();
		Criteria crit = sess.createCriteria(PurchaseOrder.class);
		if(getPurchaseOrder().getOrderdate() != null
				&& !"".equalsIgnoreCase(getPurchaseOrder().getOrderdate())){
			crit.add(Expression.like("orderdate", getPurchaseOrder().getOrderdate(),
					MatchMode.ANYWHERE));
		}
		
		
		
		resultRows = crit.list().size();
		maxPage = resultRows / maxRowPerPage;	
		prevPage = currPage - 1;
		nextPage = currPage + 1;
		page = currPage + 1;
		if (resultRows % maxRowPerPage == 0) maxPage = maxPage -1;
		setPurchaseOrders(crit.addOrder(Order.asc(orderBy))
				.setFirstResult(currPage * maxRowPerPage)
				.setMaxResults(maxRowPerPage)
				.list());
		try {
			hsf.endSession((sess));
hsf.closeSession(sess);
} catch (HibernateException e) {
e.printStackTrace();
} catch (SQLException e) {

e.printStackTrace();
}
return SUCCESS;
}
public void setHibernateSessionFactory(HibernateSessionFactory hsf) {
this.hsf = hsf;
}
public int getMaxPage() {
return maxPage;
}
public void setMaxPage(int maxPage) {
this.maxPage = maxPage;
}
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}

public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getPrevPage() {
return prevPage;
}
public void setPrevPage(int prevPage) {
this.prevPage = prevPage;
}
public int getMaxRowPerPage() {
return maxRowPerPage;
}
public void setMaxRowPerPage(int maxRowPerPage) {
this.maxRowPerPage = maxRowPerPage;
}
public String getOrderBy() {

return orderBy;
}
public void setOrderBy(String orderBy) {
this.orderBy = orderBy;
}
public int getResultRows() {
return resultRows;
}
public void setResultRows(int resultRows) {
this.resultRows = resultRows;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
}
